home *** CD-ROM | disk | FTP | other *** search
/ boe.pres.k12.wv.us / boe.pres.k12.wv.us.zip / boe.pres.k12.wv.us / Utilities / Xerox Workcentre 5335 / Windows Utilities / Font Management Utility / ChineseS / Xerox Font Management Utility.msi / Data1.cab / fmu.chm3 / skinsupport / madcaphighlighter.js < prev    next >
Text File  |  2011-04-21  |  7KB  |  197 lines

  1. // {{MadCap}} //////////////////////////////////////////////////////////////////
  2. // Copyright: MadCap Software, Inc - www.madcapsoftware.com ////////////////////
  3. ////////////////////////////////////////////////////////////////////////////////
  4. // <version>6.1.0.0</version>
  5. ////////////////////////////////////////////////////////////////////////////////
  6.  
  7. function FMCClearSearch( win )
  8. {
  9.     var highlights    = FMCGetElementsByClassRoot( win.document.body, "highlight" );
  10.     
  11.     for ( var i = 0; i < highlights.length; i++ )
  12.     {
  13.         var highlight    = highlights[i];
  14.         var innerSpan    = FMCGetChildNodeByTagName( highlight, "SPAN", 0 );
  15.         var text        = win.document.createTextNode( innerSpan.innerHTML );
  16.         
  17.         highlight.parentNode.replaceChild( text, highlight );
  18.     }
  19.     
  20.     gColorIndex = 0;
  21.     gFirstHighlight = null;
  22.     
  23.     // At this point, highlight nodes got replaced by text nodes. This causes nodes that were once single text nodes to
  24.     // become divided into multiple text nodes. Future attempts to highlight multi-character strings may not work
  25.     // because they may have been divided into multiple text nodes. To solve this, we merge adjacent text nodes.
  26.     
  27.     FMCMergeTextNodes( win.document.body );
  28. }
  29.  
  30. function FMCMergeTextNodes( node )
  31. {
  32.     for ( var i = node.childNodes.length - 1; i >= 1; i-- )
  33.     {
  34.         var currNode    = node.childNodes[i];
  35.         var prevNode    = currNode.previousSibling;
  36.         
  37.         if ( currNode.nodeType == 3 && prevNode.nodeType == 3 )
  38.         {
  39.             prevNode.nodeValue = prevNode.nodeValue + currNode.nodeValue;
  40.             node.removeChild( currNode );
  41.         }
  42.     }
  43.     
  44.     for ( var i = 0; i < node.childNodes.length; i++ )
  45.     {
  46.         FMCMergeTextNodes( node.childNodes[i] );
  47.     }
  48. }
  49.  
  50. function FMCApplyHighlight( win, root, term, color, caseSensitive, searchType )
  51. {
  52.     var re    = null;
  53.     
  54.     if ( searchType == "NGram" )
  55.     {
  56.         re = new RegExp( term, "g" + (caseSensitive ? "" : "i") );
  57.     }
  58.     else
  59.     {
  60.         re = new RegExp( "(^|\\s|[.,;!#$/:?'\"()[\\]{}|=+*_\\-\\\\])" + FMCEscapeRegEx( term ) + "($|\\s|[.,;!#$/:?'\"()[\\]{}|=+*_\\-\\\\])", "g" + (caseSensitive ? "" : "i") );
  61.     }
  62.  
  63.     for ( var i = root.childNodes.length - 1; i >= 0; i-- )
  64.     {
  65.         var node    = root.childNodes[i];
  66.         
  67.         FMCApplyHighlight( win, node, term, color, caseSensitive, searchType );
  68.         
  69.         if ( node.nodeType != 3 || node.parentNode.nodeName == "SCRIPT" )
  70.         {
  71.             continue;
  72.         }
  73.         
  74.         var currNode    = node;
  75.         var text        = currNode.nodeValue;
  76.         
  77.         for ( var match = re.exec( text ); match != null; match = re.exec( text ) )
  78.         {
  79.             var pos        = match.index + (searchType == "NGram" ? 0 : match[1].length);
  80.             var posEnd    = pos + term.length;
  81.             var span    = win.document.createElement( "span" );
  82.             
  83.             span.className = "highlight";
  84.             span.style.fontWeight = "bold";
  85.             span.style.backgroundColor = color.split( "," )[0];
  86.             span.style.color = color.split( "," )[1];
  87.             
  88.             var span2    = win.document.createElement( "span" );
  89.  
  90.             span2.className = "SearchHighlight" + (gColorIndex + 1);
  91.             span2.appendChild( win.document.createTextNode( text.substring( pos, posEnd ) ) );
  92.             
  93.             span.appendChild( span2 );
  94.             
  95.             currNode.nodeValue = text.substring( 0, pos );
  96.             currNode.parentNode.insertBefore( span, currNode.nextSibling );
  97.             currNode.parentNode.insertBefore( win.document.createTextNode( text.substring( posEnd, text.length ) ), span.nextSibling );
  98.             
  99.             currNode = currNode.nextSibling.nextSibling;
  100.             text = currNode.nodeValue;
  101.             
  102.             //
  103.             
  104.             if ( gFirstHighlight == null || span.offsetTop < gFirstHighlight.offsetTop )
  105.             {
  106.                 gFirstHighlight = span;
  107.             }
  108.             
  109.             //
  110.             
  111.             FMCUnhide( win, span );
  112.         }
  113.     }
  114. }
  115.  
  116. function FMCHighlight( win, term, color, caseSensitive, searchType )
  117. {
  118.     if ( term == "" )
  119.     {
  120.         return;
  121.     }
  122.  
  123.     FMCApplyHighlight( win, win.document.body, term, color, caseSensitive, searchType );
  124.     
  125.     // Scroll to first highlighted term
  126.     
  127.     if ( gFirstHighlight && gFirstHighlight.offsetTop > win.document.documentElement.clientHeight )
  128.     {
  129.         win.document.documentElement.scrollTop = gFirstHighlight.offsetTop;
  130.     }
  131. }
  132.  
  133. function FMCHighlightUrl( win )
  134. {
  135.     gColorIndex = 0;
  136.     gFirstHighlight = null;
  137.     
  138.     var url = win.document.location.search;
  139.     
  140.     if ( String.IsNullOrEmpty( url ) )
  141.     {
  142.         return;
  143.     }
  144.     
  145.     url = decodeURIComponent( url );
  146.  
  147.     var pos            = url.indexOf( "SearchType" );
  148.     var ampPos        = -1;
  149.     var searchType    = null;
  150.     
  151.     if ( pos >= 0 )
  152.     {
  153.         ampPos = url.indexOf( "&", pos );
  154.         searchType = url.substring( 1 + pos + "SearchType".length, ampPos >= 0 ? ampPos : url.length );
  155.     }
  156.     
  157.     pos = url.indexOf( "Highlight" );
  158.     
  159.     if ( pos >= 0 )
  160.     {
  161.         ampPos = url.indexOf( "&", pos );
  162.         
  163.         var highlight    = url.substring( 1 + pos + "Highlight".length, ampPos >= 0 ? ampPos : url.length );
  164.         var stems        = highlight.split( "||" );
  165.         
  166.         for ( var i = 0; i < stems.length; i++ )
  167.         {
  168.             var phrases    = stems[i].split( "|" );
  169.             
  170.             for ( var j = 0; j < phrases.length; j++ )
  171.             {
  172.                 FMCHighlight( win, phrases[j], gColorTable[gColorIndex], false, searchType );
  173.             }
  174.             
  175.             gColorIndex = (++gColorIndex) % 10;
  176.         }
  177.     }
  178. }
  179.  
  180. if ( gRuntimeFileType == "Default" || ((FMCIsDotNetHelp() || FMCIsHtmlHelp()) && gRuntimeFileType == "Topic") )
  181. {
  182.  
  183. var gColorTable        = new Array( "#ffff66,#000000",
  184.                                  "#a0ffff,#000000",
  185.                                  "#99ff99,#000000",
  186.                                  "#ff9999,#000000",
  187.                                  "#ff66ff,#000000",
  188.                                  "#880000,#ffffff",
  189.                                  "#00aa00,#ffffff",
  190.                                  "#886800,#ffffff",
  191.                                  "#004699,#ffffff",
  192.                                  "#990099,#ffffff" );
  193. var gColorIndex        = 0;
  194. var gFirstHighlight    = null;
  195.  
  196. }
  197.